package util;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyClassLoader extends ClassLoader {
    @Override
    protected Class<?> findClass(String name) {
        //D:\app\作业\spring\spring相关代码代码\代码\tomcat_server\out\production\tomcat_server
        String myPath = "file:///D:/app/aaa/a/"+ name.replace(".","/") + ".class"; //"file:///Users/allen/Desktop/" + name.replace(".","/") + ".class";
        System.out.println(myPath);
        byte[] cLassBytes = null;
        Path path = null;
        try {
            path = Paths.get(new URI(myPath));
            cLassBytes = Files.readAllBytes(path);
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
        Class clazz = defineClass(name, cLassBytes, 0, cLassBytes.length);
        return clazz;
    }


    public static void main(String[] args){
        MyClassLoader m=new MyClassLoader();
        Class<?> aClass =m.findClass("server.Test");
        try {
            Object obj = aClass.newInstance();
            Method method = aClass.getMethod("test");
            method.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}